1741F - Multi-Colored Segments - CodeForces Solution


binary search data structures math sortings *2000

Please click on ads to support us..

Python Code:

t = int(input())
def dist(i,j):
    if i >= j:
        return 0
    else:
        return j-i

while t > 0:
    n = int(input())
    seg = []
    for i in range(n):
        seg.append(list(map(int,input().split()))+[i+1])
    seg.sort(key=lambda x:(x[0],x[1]))
    color = {}
    i = 0
    while i < n:
        if seg[i][2] in color:
            if seg[i][2] == seg[i-1][2]:
                j = i
                while j < n-1 and seg[j][2] == seg[j+1][2]:
                    j = j + 1
                color[seg[i][2]][-1][1] = j+1
                for k in range(i,j+1):
                    color[seg[i][2]].append([k,j+1])
                i = j+1
                continue
            else:
                color[seg[i][2]].append([i,i+1])
                i = i + 1
        else:
            color[seg[i][2]] = [[i,i+1],]
            i = i + 1
    mindis= [1000000000]*(n+1)
    signal = [0]*(n+1)
    nowmaxj = [[0,0],[0,0]]
    i = 0
    while i < n:
        nowcolor = seg[i][2]
        u = signal[nowcolor]
        end = color[nowcolor][u][1]
        signal[nowcolor] += end - i
        maxj = 0
        if end != n:
            for j in range(i,end):
                maxj = max(maxj,seg[j][1])
                mindis[seg[j][3]] = min(mindis[seg[j][3]],dist(seg[j][1],seg[end][0]))
                for k in range(2):
                    if nowmaxj[k][1] != 0 and nowmaxj[k][1] != nowcolor:
                        mindis[seg[j][3]] = min(mindis[seg[j][3]],dist(nowmaxj[k][0],seg[j][0]))
        else:
            for j in range(i,end):
                for k in range(2):
                    if nowmaxj[k][1] != 0 and nowmaxj[k][1] != nowcolor:
                        mindis[seg[j][3]] = min(mindis[seg[j][3]],dist(nowmaxj[k][0],seg[j][0]))
            break
        if maxj > nowmaxj[0][0]:
            if nowmaxj[0][1] == nowcolor:
                nowmaxj[0][0] = maxj
            else:
                nowmaxj[1][0] = nowmaxj[0][0]
                nowmaxj[1][1] = nowmaxj[0][1]
                nowmaxj[0] = [maxj,nowcolor]
        elif maxj > nowmaxj[1][0]:
            if nowcolor != nowmaxj[0][1]:
                nowmaxj[1] = [maxj,nowcolor]
        i = end

    print(" ".join(map(str,mindis[1:])))
    t = t - 1


Comments

Submit
0 Comments
More Questions

1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase